//
//  Store2Header.m
//  KYRR
//
//  Created by kuyuZJ on 2017/4/21.
//
//

#import "Store2Header.h"

@interface Store2Header ()
@property(nonatomic,strong) UIImageView* backgroundImg;
@property(nonatomic,strong) UIView* topView;
@property(nonatomic,strong) UIView* storeView;
@property(nonatomic,strong) UIImageView* storeLogo;
@property(nonatomic,strong) UIView* rightView;
@property(nonatomic,strong) UILabel* labelShip;
@property(nonatomic,strong) UIImageView* iconHorn;
@property(nonatomic,strong) UILabel* labelNotice;
@property(nonatomic,strong) UIView* activeView;
@property(nonatomic,strong) UILabel* labelIcon;
@property(nonatomic,strong) UILabel* labelActive;

@end

@implementation Store2Header{
    CGFloat topHeight,storeHeight;
    CGSize logoSize,iconSize;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        topHeight = 64.f;
        storeHeight = 70.f;
        logoSize = CGSizeMake(60.f, 60.f);
        iconSize = CGSizeMake(15.f, 15.f);
        [self addSubview:self.backgroundImg];
        self.backgroundImg.opaque = YES;
        self.backgroundImg.clipsToBounds = YES;
        [self addSubview:self.topView];
        [self addSubview:self.storeView];
        
        [self.storeView addSubview:self.storeLogo];
        [self.storeView addSubview:self.rightView];
        
        [self.rightView addSubview:self.labelShip];
        [self.rightView addSubview:self.iconHorn];
        [self.rightView addSubview:self.labelNotice];
        
        [self addSubview:self.activeView];
        [self.activeView addSubview:self.labelIcon];
        [self.activeView addSubview:self.labelActive];
        
        UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapNoticeTouch:)];
        [self.labelNotice addGestureRecognizer:tap];
    }
    return  self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    NSArray<NSString*>* formats = @[@"H:|-defEdge-[backgroundImg]-defEdge-|", @"V:|-defEdge-[backgroundImg]-defEdge-|",
                                    @"H:|-defEdge-[topView]-defEdge-|",@"H:|-defEdge-[storeView]-defEdge-|",@"H:|-defEdge-[activeView]-defEdge-|",
                                    @"V:|-defEdge-[topView(==topHeight)][storeView(==storeHeight)][activeView(==30)]",
                                    
                                    @"H:|-leftEdge-[storeLogo(==logoWidth)]-leftEdge-[rightView]-leftEdge-|",
                                    @"V:|-defEdge-[storeLogo(==logoHeight)]", @"V:|-defEdge-[rightView]-topEdge-|",
                                    
                                    @"H:|-defEdge-[labelShip]-defEdge-|",@"H:|-defEdge-[iconHorn(==iconWidth)][labelNotice]-defEdge-|",
                                    @"V:|-topEdge-[labelShip][iconHorn(==iconHeight)]-topEdge-|", @"V:|-topEdge-[labelShip][labelNotice(==iconHeight)]-topEdge-|",
                                    @"H:|-leftEdge-[labelIcon(==17)]-leftEdge-[labelActive]-defEdge-|", @"V:[labelIcon(==17)]", @"V:|-defEdge-[labelActive]-defEdge-|"
                                    ];
    NSDictionary<NSString*,id>* metrics = @{ @"defEdge":@(0), @"leftEdge":@(10), @"topEdge":@(10),
                                             @"topHeight":@(topHeight), @"storeHeight":@(storeHeight), @"logoWidth":@(logoSize.width), @"logoHeight":@(logoSize.height),
                                             @"shipHeight":@(20), @"iconWidth":@(iconSize.width), @"iconHeight":@(iconSize.height)
                                             };
    NSDictionary<NSString*,id>* views = @{ @"backgroundImg":self.backgroundImg,
                                           @"topView":self.topView, @"storeView":self.storeView, @"activeView":self.activeView,
                                           @"storeLogo":self.storeLogo, @"rightView":self.rightView,
                                           @"labelShip":self.labelShip, @"iconHorn":self.iconHorn, @"labelNotice":self.labelNotice,
                                           @"labelIcon":self.labelIcon, @"labelActive":self.labelActive
                                           };
    [formats enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog( @"%@ %@",[self class],obj);
        NSArray<NSLayoutConstraint*>* constraints = [NSLayoutConstraint constraintsWithVisualFormat:obj options:0 metrics:metrics views:views];
        [self addConstraints:constraints];
    }];
    [self.activeView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelIcon attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.activeView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.f]];
}

-(void)setEntity:(MStore *)entity{
    if(entity){
        _entity = entity;
        [self.storeLogo sd_setImageWithURL:[NSURL URLWithString:self.entity.logo] placeholderImage:[UIImage imageNamed:kDefStoreLogo]];
        self.labelShip.text = [NSString stringWithFormat:@"起步价 $%@ | 配送费 $%@ ",self.entity.freeShip,self.entity.shipFee];
        self.labelNotice.text = self.entity.notice;
        BOOL flag = NO;
        for (NSDictionary* sub in self.entity.arrayActive) {
            if([sub objectForKey: @"满"]){
                self.labelIcon.text =  @"减";
                self.labelActive.text = [sub objectForKey: @"满"];
                flag = YES;
            }
        }
        if(flag){
            self.activeView.hidden = NO;
        }else{
            self.activeView.hidden = YES;
        }
    }
}

#pragma mark =====================================================  SEL
-(IBAction)tapNoticeTouch:(id)sender{
    if(self.delegate && [self.delegate respondsToSelector:@selector(store2HeaderTapNotice)]){
        [self.delegate store2HeaderTapNotice];
    }
}

#pragma mark =====================================================   property package
-(UIImageView *)backgroundImg{
    if(!_backgroundImg){
        _backgroundImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed: @"icon-bg-store"]];
        _backgroundImg.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _backgroundImg;
}
-(UIView *)topView{
    if(!_topView){
        UIView* empty = [[UIView alloc]init];
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _topView = empty;
    }
    return _topView;
}

-(UIView *)storeView{
    if(!_storeView){
        UIView* empty = [[UIView alloc]init];
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _storeView = empty;
    }
    return _storeView;
}

-(UIImageView *)storeLogo{
    if(!_storeLogo){
        UIImageView* empty = [[UIImageView alloc]init];
        empty.layer.masksToBounds = YES;
        empty.layer.cornerRadius = logoSize.height/2;
        empty.layer.borderColor = [UIColor whiteColor].CGColor;
        empty.layer.borderWidth = 1.0;
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _storeLogo = empty;
    }
    return _storeLogo;
}

-(UIView *)rightView{
    if(!_rightView){
        UIView* empty = [[UIView alloc]init];
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _rightView = empty;
    }
    return _rightView;
}

-(UILabel *)labelShip{
    if(!_labelShip){
        UILabel* empty = [[ UILabel alloc]init];
        empty.textColor = [UIColor whiteColor];
        empty.font = [UIFont systemFontOfSize:14.f];
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _labelShip = empty;
    }
    return _labelShip;
}

-(UIImageView *)iconHorn{
    if(!_iconHorn){
        UIImageView* empty = [[UIImageView alloc]init];
        [empty setImage:[UIImage imageNamed:@"icon-horn"]];
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _iconHorn = empty;
    }
    return _iconHorn;
}

-(UILabel *)labelNotice{
    if(!_labelNotice){
        UILabel* empty = [[ UILabel alloc]init];
        empty.textColor = [UIColor whiteColor];
        empty.font = [UIFont systemFontOfSize:14.f];
        empty.userInteractionEnabled = YES;
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _labelNotice = empty;
    }
    return _labelNotice;
}

-(UIView *)activeView{
    if(!_activeView){
        UIView* empty = [[UIView alloc]init];
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _activeView = empty;
    }
    return _activeView;
}
-(UILabel *)labelIcon{
    if(!_labelIcon){
         UILabel* empty = [[UILabel alloc]init];
        empty.backgroundColor =  [UIColor redColor];;
        empty.font = [UIFont systemFontOfSize:14.f];
        empty.textColor = [UIColor whiteColor];
        empty.layer.masksToBounds = YES;
        empty.layer.cornerRadius = 3.f;
//        empty.layer.borderWidth = 5.f;
//        empty.layer.borderColor = theme_navigation_color.CGColor;
        empty.textAlignment = NSTextAlignmentCenter;
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _labelIcon = empty;
    }
    return _labelIcon;
}

-(UILabel *)labelActive{
    if(!_labelActive){
         UILabel* empty = [[UILabel alloc]init];
        empty.font = [UIFont systemFontOfSize:14.f];
        empty.textColor = [UIColor whiteColor];
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _labelActive = empty;
    }
    return _labelActive;
}


@end
